// createRouter 创建路由实例
// createWebHashHistory hash模式 地址有#
// createWebHistory 历史模式
import { createRouter, createWebHashHistory } from "vue-router";
import Admin from '@/layouts/admin.vue'
import Index from '@/pages/index.vue'
import NotFound from '@/pages/404.vue'
import Login from '@/pages/login.vue'
import GoodList from '@/pages/goods/list.vue'
import CategoryList from '@/pages/category/list.vue'
import UserList from '@/pages/user/list.vue'
import OrderList from '@/pages/order/list.vue'
import CommentList from '@/pages/comment/list.vue'
import ImageList from '@/pages/image/list.vue'
import NoticeList from '@/pages/notice/list.vue'
import SettingList from '@/pages/setting/base.vue'
import CouponList from '@/pages/coupon/list.vue'
import ManagerList from '@/pages/manager/list.vue'
import AccessList from '@/pages/access/list.vue'
import RoleList from '@/pages/role/list.vue'
import SkusList from '@/pages/skus/list.vue'





// export const routes = [
//     {
//         path: "/",
//         component: Admin,
//         // 子路由
//         children: [{
//             path: "/",
//             component: Index,
//             meta: {
//                 title: "后台首页"

//             }
//         }, {
//             path: "/goods/list",
//             component: GoodList,
//             meta: {
//                 title: "商品管理"

//             }
//         }, {
//             path: "/category/list",
//             component: CategoryList,
//             meta: {
//                 title: "分类列表"

//             }
//         }]
//     },
//     {
//         path: "/login",
//         name: "login",
//         component: Login,
//         meta: {
//             title: "登录页面"

//         }
//     },
//     {
//         path: '/:pathMatch(.*)*',
//         name: 'NotFound',
//         component: NotFound

//     }]


export const routes = [
    {
        path: "/",
        name: "admin",
        component: Admin,
    },
    {
        path: "/login",
        name: "login",
        component: Login,
        meta: {
            title: "登录页面"

        }
    },
    {
        path: '/:pathMatch(.*)*',
        name: 'NotFound',
        component: NotFound

    }]

// 动态路由, 用于匹配菜单

const asyncRoutes = [{
    path: "/",
    name: "/",
    component: Index,
    meta: {
        title: "后台首页"

    }
}, {
    path: "/goods/list",
    name: "/goods/list",
    component: GoodList,
    meta: {
        title: "商品管理"

    }
}, {
    path: "/category/list",
    name: "/category/list",
    component: CategoryList,
    meta: {
        title: "分类列表"

    }
}, {
    path: "/user/list",
    name: "/user/list",
    component: UserList,
    meta: {
        title: "用户列表"

    }
}, {
    path: "/order/list",
    name: "/order/list",
    component: OrderList,
    meta: {
        title: "订单列表"

    }
},{
    path: "/comment/list",
    name: "/comment/list",
    component: CommentList,
    meta: {
        title: "评价列表"

    }
}, {
    path: "/image/list",
    name: "/image/list",
    component: ImageList,
    meta: {
        title: "图片列表"

    }
}, {
    path: "/notice/list",
    name: "/notice/list",
    component: NoticeList,
    meta: {
        title: "公告列表"

    }
}, {
    path: "/setting/base",
    name: "/setting/base",
    component: SettingList,
    meta: {
        title: "设置列表"

    }
}, {
    path: "/coupon/list",
    name: "/coupon/list",
    component: CouponList,
    meta: {
        title: "优惠券列表"

    }
}, {
    path: "/manager/list",
    name: "/manager/list",
    component: ManagerList,
    meta: {
        title: "管理员管理"

    }
},{
    path: "/access/list",
    name: "/access/list",
    component: AccessList,
    meta: {
        title: "菜单权限管理"

    }
},{
    path: "/role/list",
    name: "/role/list",
    component: RoleList,
    meta: {
        title: "角色管理"
    }
},{
    path: "/skus/list",
    name: "/skus/list",
    component: SkusList,
    meta: {
        title: "规格管理"
    }
}]



// 创建路由实例 导入配置项和模式
export const router = createRouter({
    routes,  // 路由配置项
    history: createWebHashHistory(), // 模式
})



// 动态添加路由
export function addRoutes(menus) {
    // 是否有新的路由
    let hasNewRoutes = false
    const findAndRoutesByMenus = (arr) => {
        arr.forEach(e => {
            let itemIndex = asyncRoutes.findIndex(o => o.path == e.frontpath);
            if (itemIndex !== -1 && !router.hasRoute(asyncRoutes[itemIndex].path)) {
                router.addRoute("admin", asyncRoutes[itemIndex]);
                hasNewRoutes = true
            }

            if (e.child && e.child.length > 0) {
                findAndRoutesByMenus(e.child);
            }
        });
    };

    findAndRoutesByMenus(menus);
    return hasNewRoutes
}